package com.lgt_enterprise_android.view.pages.me;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.lgt_enterprise_android.R;
import com.lgt_enterprise_android.base.BaseActivity;
import com.lgt_enterprise_android.utils.BasicHttp;
import com.lgt_enterprise_android.utils.CommonUtil;
import com.lgt_enterprise_android.utils.HttpManager;
import com.lgt_enterprise_android.utils.StringUtil;
import com.lgt_enterprise_android.view.MainActivity;
import com.lgt_enterprise_android.view.dialogs.SexPickerDialog;
import com.lgt_enterprise_android.view.fragments.MeFragment;
import com.widgets.CustomizeTitleView;

public class ProfileInfoActivity extends BaseActivity {

    private TextView txtSex;
    private TextView txtName;
    private TextView txtPhone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile_info);

        if(MeFragment.jsonProfileInfo==null){
            return;
        }

        txtSex = (TextView) findViewById(R.id.txt_sex);
        txtName = (TextView) findViewById(R.id.txt_name);
        txtPhone = (TextView) findViewById(R.id.txt_phone);

        String strTitle=MeFragment.jsonProfileInfo.getString("name");
        if(StringUtil.isEmpty(strTitle)){
            strTitle=MeFragment.jsonProfileInfo.getString("phoneNumber");
        }
        txtName.setText(strTitle);

        int gender=MeFragment.jsonProfileInfo.getInteger("gender");

        if (gender == 1) {
            txtSex.setText("男");
            txtSex.setTag(1);
        } else if(gender == 2){
            txtSex.setText("女");
            txtSex.setTag(2);
        }

        txtPhone.setText(MeFragment.jsonProfileInfo.getString("phoneNumber"));
        ((TextView) findViewById(R.id.me_txt_name)).setText(txtName.getText().toString());


        findViewById(R.id.lin_name).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, EditInfoActivity.class);
                intent.putExtra("type", EditInfoActivity.USER_NAME);
                intent.putExtra("val", txtName.getText().toString());
                startActivityForResult(intent, EditInfoActivity.USER_NAME);
            }
        });


        findViewById(R.id.lin_sex).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SexPickerDialog sexPickerDialog = new SexPickerDialog();
                sexPickerDialog.setPickerDialogCallback(new SexPickerDialog.IChangeSucess() {
                    @Override
                    public void changeSex(int gender) {
                        if (gender == 1) {
                            txtSex.setText("男");
                            txtSex.setTag(1);
                        } else if(gender == 2){
                            txtSex.setText("女");
                            txtSex.setTag(2);
                        }
                    }
                });
                sexPickerDialog.show(getSupportFragmentManager(), "SexPickerDialog");
            }
        });

        if(MeFragment.jsonProfileInfo!=null){
            Glide.with(mContext).applyDefaultRequestOptions(CommonUtil.getCircelGlideOptions()).load(MeFragment.jsonProfileInfo.getString("avatarUrl")).into((ImageView)findViewById(R.id.me_img_head));
        }


        CustomizeTitleView customizeTitleView=(CustomizeTitleView)findViewById(R.id.customizeTitleView);
        customizeTitleView.setMoreListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                JSONObject jo=new JSONObject();

                String stringName=txtName.getText().toString();
                if(StringUtil.isEmpty(stringName)){
                    showToast("姓名不能为空");
                    return;
                }

                jo.put("gender",txtSex.getTag().toString());
                jo.put("name",txtName.getText().toString());
                jo.put("phoneNumber",txtPhone.getText().toString());

                HttpManager.saveProfileInfo(mContext, jo.toJSONString(), new BasicHttp.IMyCallBack() {
                    @Override
                    public void onResponse(Object resonseObj) {
                        showToast("保存成功");
                        Intent intent=new Intent(mContext, MainActivity.class);
                        startActivity(intent);
                    }

                    @Override
                    public void onFailure(String mes) {

                    }
                });

            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode != RESULT_OK) return;
        if (EditInfoActivity.USER_NAME == requestCode) {
            txtName.setText(data.getStringExtra("val"));
        }
    }
}
